#include "fonctions.h"



// int active_threads = 0;
// typedef struct paramThread paramThread;
// struct paramThread {
// 	int 	descripteur;
// 	struct 	in_addr	adresse;
// };



// void * thread(void *arg) {
// 	printf("Start Thread.\n");
// 	int n;
// 	int i;
// 	paramThread *params = (paramThread*) arg;
// 	int cnxsd = (int) params->descripteur;
// 	struct in_addr sin_addr = (struct in_addr) params->adresse;
// 	char msgbuf[MAX_MSG];
// 	char *ptr;
// 	while((n = f_read(cnxsd, msgbuf, MAX_MSG)) > 0) {
// 		i=0;
// 		printf("RECU: Received %2d bytes from %s:", n, inet_ntoa(sin_addr));
// 		msgbuf[n] = '\0';
// 		for(ptr = msgbuf; n > 0; n--) {
// 			printf("%c", *ptr++);
// 			i++;
// 		}
// 		if (strcmp(msgbuf, "quit\r\n") == 0) {
// 			printf("Demande de quittement recue.\n");
// 			close(cnxsd);
// 			active_threads--;
// 			printf("Stop Thread.\n");
// 			pthread_exit(NULL);
// 		}
// 	}
// 	if(n < 0) {
//         perror("read");
//     }
//     close(cnxsd);
//     active_threads--;
// 	printf("Stop Thread.\n");
// 	pthread_exit(NULL);
// }



int main(int argc, char const *argv[]) {
	struct SocketTCP* socket;
    struct sockaddr_in adresse_client;
    int cnxsd;
    //pthread_t tab[5];
    //paramThread *lesParam;
    socklen_t addr_len;
    int n;
    char msgbuf[MAX_MSG], *ptr;
    char msgresponse[MAX_MSG] ="Message from Server => well Received\n";

    // Creation socket.
    socket = creatSocketTCP();
    // Initialisation socket.
    socket->new_addr->sin_family = AF_INET;
    (socket->new_addr->sin_addr).s_addr = htonl(INADDR_ANY);
    socket->new_addr->sin_port = htons(SERVER_PORT);

    // Bind.
    if(bind(socket->descripteur, (struct sockaddr *) socket->new_addr, socket->new_addr_len)) {
        perror("ERREUR: Server - bind.");
        return -1;
    }

    // Listen.
    if (f_listen(socket->descripteur, 5) == -1) {
    	perror("ERREUR: Server - listen.");
		return 1; 
    }

    // Communication.
    while(1) {
		addr_len = sizeof (adresse_client);
		puts("Waiting for connection...");
		if((cnxsd = f_accept(socket->descripteur, (struct sockaddr*) &adresse_client, &addr_len)) == 1){
			perror("ERROR: SERVER @ f_accept == 1");
			continue;
		} else {
			printf("PORT SERVEUR ECOUTE: %d\n", socket->new_addr->sin_port);
			// sendto()
			printf("Connection accepted from %s\n",inet_ntoa(adresse_client.sin_addr));
			// while((n = f_read(socket->descripteur, msgbuf, MAX_MSG)) > 0) {
			while((n = f_read(cnxsd, msgbuf, MAX_MSG)) > 0) {
				msgbuf[n]= '\0'; //vide le buffer pour pouvoir faire quit
				printf("Received %2d bytes from %s:", n,inet_ntoa(adresse_client.sin_addr));
				for(ptr = msgbuf; n > 0; n--) { 
					printf("%c",*ptr++);
					write(cnxsd,msgresponse,MAX_MSG);
				}
				if(strcmp (msgbuf,"quit\r\n")== 0) {
					printf("GOODBYE BRO !\n");
					exit(0);
				}
			}
		}
		if (n < 0) {
			perror("read");
			close(cnxsd);
		}
	}




 //    if ((sd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
	// {
	// 	perror("socket creation");
	// 	return 1;
	// }

 //    // Eliberation socket apres fin programme.
	// int on;
	// on = 1;
	// setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); 
    
 //    // Bind socket 
 //    server_addr.sin_family 		= AF_INET;
 //    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 //    server_addr.sin_port 		= htons(SERVER_PORT);
 //    if(bind(sd, (struct sockaddr*) &server_addr, sizeof server_addr) == -1) { 
 //        perror("bind");
 //        return 1;
 //    }
    
 //    // Put socket in listen mode
 //    if (f_listen(sd, 5) == -1) {
 //    	perror("listen");
	// 	return 1; 
 //    }

 //    // Deal with clients.
 //    while(1) {
 //        addr_len = sizeof addr;
 //        puts("Waiting for connection...");
 //        if((cnxsd = f_accept (sd, (struct sockaddr*) &addr, &addr_len)) == -1) {
 //            perror("accept");
 //            continue;
 //        } else {
 //        	if (active_threads < 5) {
 //        		printf("++++++++++ 1");
 //        		active_threads++;
	//             printf("Connection accepted from %s\n",inet_ntoa(addr.sin_addr));
	//             // Create a thread for the connection.
	//             lesParam = malloc (sizeof(paramThread));
	//             lesParam->descripteur = cnxsd;
	//             lesParam->adresse 	 = addr.sin_addr;
	//             if (pthread_create(&tab[active_threads - 1], NULL, thread, (void *) lesParam) != 0) {
	// 				printf("Erreur de creation du thread no.%d\n", active_threads);
	// 				exit(-1);
	// 			}
 //            }
 //        }
	// }
	return 0;
}


